//算特定容器活动高度  totalH 总高度 类型 int obj ; otherH 待减去的高度 类型 int obj ; number 修正偏差 类型 int
jQuery.fn.flexHeight = function(obj) {

    //获取总高度
    if(!obj.totalH){
        totalH=$(window).height();
    }else if(typeof(obj.totalH)=="number"){
        totalH=obj.totalH;
    }else if(typeof(obj.totalH)=="object"){
        totalH=obj.totalH.height();
    };

    //获取需要减去对象的高度
    if(!obj.otherH){
        otherH=0;
    }else if(typeof(obj.otherH)=="number"){
        otherH=obj.otherH;
    }else if(typeof(obj.otherH)=="object"){
        var otherH=0;
        $.each(obj.otherH, function(i, n){
           otherH=otherH+$(n).height() ;

        });
    };
    //获取偏差修正值
    if(!obj.number){obj.number=0};


    //计算高度
    $(this).each(function(index, element) {
        $(element).height(totalH-otherH-obj.number);

    });
}

function processResp(resp, successFn, failedFn){
    if(!resp){
        showModal('no response!');
        return;
    }
    if(resp.status == 1 ||resp.status =='1'){
        if(successFn && typeof successFn == 'function'){
            successFn(resp.msg,resp);
            if(resp.redirect){
                setTimeout(function(){
                    location.href = resp.redirect;
                },1000)
            }
        } else {
            showModal({
                msg:resp.msg,
                callbackTime:1000,
                end:function(){
                    if(resp.redirect){
                        location.href = resp.redirect;
                    } else {
                        location.reload();
                    }
                }
            });
        }
    } else {
        if(failedFn && typeof failedFn == 'function'){
            failedFn(resp.msg,resp);
            if(resp.redirect){
                setTimeout(function(){
                    location.href = resp.redirect;
                },1000)
            }
        } else {
            showModal({
                msg:resp.msg,
                callbackTime:1000,
                end:function(){
                    if(resp.redirect){
                        location.href = resp.redirect;
                    }
                }
            });
        }
    }
    hideLoading();
}

function Nsubmit(form){
    if(!form){
        showModal({msg:'no form'});
    }
    if(typeof form == 'string'){
        form = $('<form action="'+form+'" method="post"><form>');
    }
    form.Qvalidate({
        onError:function(element,msg){
            hideLoading();
            $(element).addClass("input-error");
            if($(element)[0].tagName=='SELECT'){
               //错误控件为select2时处理
              $(element).next().find(".select2-selection").addClass("input-error");
            };
            if($(element).attr('contenteditable')){
               //错误控件为contenteditable时处理

              $(element).prev().addClass("input-error");
            };
            showModal({
                msg:msg
            });
        },
        onSuccess:function(element,msg){
            $(element).removeClass("input-error");
            if($(element)[0].tagName=='SELECT'){
               //错误控件为select2时处理
              $(element).next().find(".select2-selection").removeClass("input-error");
            };
            if($(element).attr('contenteditable')){
               //错误控件为contenteditable时处理
             
              $(element).prev().removeClass("input-error");
            }

        }
    });  
    if(form.Qvalidate.valid()){
        form.submit();
    }  
}

function Qsubmit(form,successFn,failedFn,beforeSubmit){
    console.log(beforeSubmit);
    if(!form){
        showModal({msg:'no form'});
    }
    var parent = null;
    if(typeof form == 'string'){
        form = $('<form action="'+form+'" method="get"><form>');
        parent = $('.right');
    } else {
        parent = form.parent('div');
    }
    form.Qvalidate({
        onError:function(element,msg){
            hideLoading();
            $(element).addClass("input-error");
            if($(element)[0].tagName=='SELECT'){
               //错误控件为select2时处理
              $(element).next().find(".select2-selection").addClass("input-error");
            };
            if($(element).attr('contenteditable')){
               //错误控件为contenteditable时处理

              $(element).prev().addClass("input-error");
            };
            showModal({
                msg:msg
            });
        },
        onSuccess:function(element,msg){
            $(element).removeClass("input-error");
            if($(element)[0].tagName=='SELECT'){
               //错误控件为select2时处理
              $(element).next().find(".select2-selection").removeClass("input-error");
            };
            if($(element).attr('contenteditable')){
               //错误控件为contenteditable时处理
             
              $(element).prev().removeClass("input-error");
            }

        },
        ignore: (beforeSubmit && beforeSubmit.ignore ) ? beforeSubmit.ignore : ""
    });  
    if(form.Qvalidate.valid()){
        if($(".loading").length==0){
            parent.showLoading();
        }

        form.ajaxSubmit({
                success:function(resp){
                    processResp(resp, successFn, failedFn);
                }
        });
    }else{
        hideLoading();
        $(".animated").addClass("shake");
    };       

    setTimeout(function () {
        $(".animated").removeClass("shake");
    }, 1000);

}

function U(url, vars){
    if(!url || url.length == 0){
        return location.pathname;
    }
    var curi = parseUrl();
    if(url.indexOf('/')==0){
        if(url.length>0){
            return url+curi['SUFFIX_NAME'];
        } else {
            return url;
        }
    }
    var u = '';
    var len = url.split('/').length;
    if(len == 1){
        u = '/'+curi['MODULE_NAME']+'/'+curi['CONTROLLER_NAME']+'/'+url;
    } else if(len == 2){
        u = '/'+curi['MODULE_NAME']+'/'+url;
    } else {
        u = '/'+url;
    }
    var p = '';
    if(typeof vars == 'object' || typeof vars == 'array' ){
        for(var k in vars){
            if(typeof vars[k] != 'function'){
                p += '/'+k+'/'+vars[k];
            }
        }
    }
    return u+p+curi['SUFFIX_NAME'];
}

function parseUrl(){
    var p = location.pathname.split('/');
    var ext = location.pathname.split('.');
    var ret = [];
    if(p.length == 1){
        return ret;
    }
    if(p.length > 1){
        ret['MODULE_NAME'] = p[1];
    }
    if(p.length > 2){
        ret['CONTROLLER_NAME'] = p[2];
    } else {
        ret['CONTROLLER_NAME'] = 'Index';
    }
    if(p.length > 3){
        ret['ACTION_NAME'] = p[3];
    }
    if(ext.length>1){
        ret['SUFFIX_NAME'] = '.'+ext[ext.length-1];
    } else {
        ret['SUFFIX_NAME'] = '';
    }
    return ret;
}

function hideLoading(){
    if($(".loading").length>0){
        $(".loading").parent().hideLoading();
    }
}

function isArray(o) {
    return Object.prototype.toString.call(o) === '[object Array]';
}

function fillForm(beanArray,beanName){
    for(var key in beanArray){
        var temp = $('[name="'+beanName+'['+key+']"]');
        var value =beanArray[key];
        if(temp.length>0){
            var type = temp.attr('type');
            temp.attr('type')
            if(type == 'hidden' || type == 'text' || type == 'textarea'){
                temp.val(value);
                if(temp.attr('contenteditable')){
                    // $(temp).prev().html(value);
                    temp.syncContent({value:value});
                }
            }else if(type == undefined){
                if(temp.hasClass('select2')){
                     temp.val(value).trigger("change");
                }
            }
        }
    }
}